package io.gitee.xyuesheng.day07;

import org.junit.Test;

public class TestRecursion {
    @Test
    public void testFabonacii() {
        //1 1 2 3 5 8 13 21 34 55
        for (int i = 0; i < 10; i++) {
            System.out.print(fab(i) + " ");
        }

    }

    public int fab(int index) {
        if (index == 0 || index == 1) {//递归的终止条件
            return 1;
        } else {
            //递归公式
            return fab(index - 1) + fab(index - 2);
        }
    }

    @Test
    public void testSum() {
        System.out.println(sum(100));
    }

    //sum(n)=sum(n-1)+n
    public int sum(int num) {
//        int sum=0;
//        for(int i=1;i<=num;i++){
//            sum+=i;
//        }
//        return sum;
//    }

        if (num == 1) {
            return 1;
        } else {
            return sum(num - 1) + num;
        }
    }

    @Test
    //求n的阶乘
    public void testNum() {
        System.out.println(num(5));
    }

    public int num(int n) {
        if (n == 1) {
            return 1;
        } else {
            return num(n - 1) * n;
        }
    }

    @Test
    public static void main(String[] args) {
        int array[] = {6, 1, 5, 4, 8, 3, 9, 12, 51, 11, 15};
        System.out.print(maxNum(array, array.length - 1));
    }

    public static int maxNum(int[] arr, int end) {
        if (end == 0) {
            return arr[0];
        }
        return max(arr[end], maxNum(arr, end - 1));
    }

    private static int max(int a, int b) {
        return a > b ? a : b;
    }

}
